#region Apache License
//
// Licensed to the Apache Software Foundation (ASF) under one or more 
// contributor license agreements. See the NOTICE file distributed with
// this work for additional information regarding copyright ownership. 
// The ASF licenses this file to you under the Apache License, Version 2.0
// (the "License"); you may not use this file except in compliance with 
// the License. You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//
#endregion



using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using IPSim.Util;
using IPSim.Core;
using IPSim.Data;
using IPSim.Devices;
using WeifenLuo.WinFormsUI.Docking;


namespace IPSim.GUI
{
    public partial class MainForm : Form
    {
        private LogerForm logerForm = new LogerForm();
        private DesignerForm designerForm = new DesignerForm();
        private ConsoleForm consoleForm = new ConsoleForm();
        private PropertiesForm propertiesForm = new PropertiesForm();

        public void HideLogScreen()
        {
            if (logerForm!=null)
                logerForm.Hide();
        }

        public void ShowLogScreen()
        {
            if(logerForm==null || logerForm.IsDisposed)
            {
                logerForm = new LogerForm();
            }
            logerForm.Location = new Point(this.Location.X + this.Width, this.Location.Y);
            logerForm.Show(dockPanel1, DockState.DockBottom);
        }

        public void HideConsoleScreen()
        {
            if (consoleForm != null)
                consoleForm.Hide();
        }

        public void ShowConsoleScreen()
        {
            if (consoleForm == null || consoleForm.IsDisposed)
            {
                consoleForm = new ConsoleForm();
            }
            consoleForm.Show(dockPanel1, DockState.DockBottom);
        }

        public void HidePropertiesScreen()
        {
            if (propertiesForm != null)
                propertiesForm.Hide();
        }

        public void ShowPropertiesScreen()
        {
            if (propertiesForm == null || propertiesForm.IsDisposed)
            {
                propertiesForm = new PropertiesForm();
            }
            propertiesForm.Show(dockPanel1, DockState.DockRight);
        }

        public MainForm()
        {
            InitializeComponent();
        }
        /// <summary>
        /// The main entry point for the application.
        /// </summary>
        /// 

        [STAThread]
        static void Main()
        {
            Application.EnableVisualStyles();
            Application.DoEvents();
            Application.Run(new MainForm());
        }

        private void MainForm_Load(object sender, EventArgs e)
        {
            propertiesForm.Show(dockPanel1, DockState.DockRight);
            logerForm.Show(dockPanel1, DockState.DockBottom);
            consoleForm.Show(dockPanel1, DockState.DockBottom);
            consoleForm.DockTo(logerForm.Pane, DockStyle.Left, 0);            
            designerForm.Show(dockPanel1, DockState.Document);
            designerForm.DockTo(dockPanel1, DockStyle.Fill);
            designerForm.NetworkDesigner.NetworkDesign.ShowGrid = true;
            designerForm.NetworkDesigner.NetworkDesign.PropertyChanged += new EventHandler(design_PropertyChanged);
            design_PropertyChanged(this, new EventArgs());
            ShowLogScreen();
            toolStripButton4.Checked = true;
            toolStripButton5.Checked = true;
            toolStripButton6.Checked = true;
            toolStripButton7.Checked = true;
            TestClient();
        }


        private void TestClient()
        {
            Logger.log("***Starting TEST CLIENT***", LogLevel.INFO);

            IPAddress ipAddr1 = new IPAddress(100);
            IPAddress ipAddr2 = new IPAddress(101);
            IPAddress ipAddr3 = new IPAddress(102);
            IPAddress ipAddr4 = new IPAddress(104);
            IPAddress ipAddr5 = new IPAddress(105);
            IPAddress ipAddr6 = new IPAddress(106);
            IPAddress ipAddr7 = new IPAddress(107);
            IPAddress ipAddr8 = new IPAddress(108);
            IPAddress ipAddr9 = new IPAddress(109);
            IPAddress ipAddr10 = new IPAddress(110);
            IPAddress ipAddr11 = new IPAddress(111);
            IPAddress ipAddr12 = new IPAddress(112);

            Computer comp1 = new Computer(ipAddr1, new IPAddress(100));
            Computer comp2 = new Computer(ipAddr2, new IPAddress(100));
            Router router1 = new Router(ipAddr1, ipAddr2, ipAddr3, ipAddr4, new IPAddress(100));
            Router router2 = new Router(ipAddr5, ipAddr6, ipAddr7, ipAddr8, new IPAddress(100));
            Router router3 = new Router(ipAddr9, ipAddr10, ipAddr11, ipAddr12, new IPAddress(100));


            Hub hub1 = new Hub();
            Hub hub2 = new Hub();
            Hub hub3 = new Hub();

            //Connector.Connect(comp1, hub1);
            //Connector.Connect(hub1, hub2);
            //Connector.Connect(comp2, hub2);
            Connector.Connect(router1, router2, 2, 2);
            Connector.Connect(router3, router2, 1, 1);
            Connector.Connect(comp1, router1, 1, 3);
            Connector.Connect(comp1, router3, 1, 3);
            


            UDPPacket udpPaket = new UDPPacket(10,10);
            TCPPacket tcpPaket = new TCPPacket(20,20);

            /*comp1.SendUDPPacket(udpPaket, ipAddr2);
            comp2.SendUDPPacket(udpPaket, ipAddr1);
            comp1.SendTCPPacket(tcpPaket, ipAddr2);
            comp2.SendTCPPacket(tcpPaket, ipAddr1);*/

            Logger.log("TC Started!!!!!!!!!!!!!!!!!!");

        }

        private void MainForm_FormClosed(object sender, FormClosedEventArgs e)
        {
            Environment.Exit(0);
        }

        private void exitToolStripMenuItem_Click(object sender, EventArgs e)
        {
            Environment.Exit(0);
        }

        private void aboutToolStripMenuItem_Click(object sender, EventArgs e)
        {
            AboutForm aboutForm = new AboutForm();
            aboutForm.ShowDialog(this);
        }

        private void toolStripButton1_Click(object sender, EventArgs e)
        {
            this.toolStripButton2.Checked = false;
            this.toolStripButton3.Checked = false;
            this.computerToolStripMenuItem.Checked = false;
            this.routerToolStripMenuItem.Checked = false;
            this.switchToolStripMenuItem.Checked = false;
            this.hubToolStripMenuItem.Checked = false;            

            Logger.log("Action Select");
            designerForm.NetworkDesigner.NetworkDesign.Action = DesignerAction.Select;
        }

        private void computerToolStripMenuItem_Click(object sender, EventArgs e)
        {
            this.toolStripDropDownButton1.Image = IPSim.Properties.Resources.computer32;
            this.toolStripButton1.Checked = false;
            this.toolStripButton2.Checked = false;
            this.toolStripButton3.Checked = false;
            this.routerToolStripMenuItem.Checked = false;
            this.switchToolStripMenuItem.Checked = false;
            this.hubToolStripMenuItem.Checked = false;  

            Logger.log("Action ADD - Computer");
            designerForm.NetworkDesigner.NetworkDesign.Action = DesignerAction.Add;
            designerForm.NetworkDesigner.NetworkDesign.DeviceType = DeviceType.Computer;
        }

        private void routerToolStripMenuItem_Click(object sender, EventArgs e)
        {
            this.toolStripDropDownButton1.Image = IPSim.Properties.Resources.router32;
            this.toolStripButton1.Checked = false;
            this.toolStripButton2.Checked = false;
            this.toolStripButton3.Checked = false;
            this.computerToolStripMenuItem.Checked = false;
            this.switchToolStripMenuItem.Checked = false;
            this.hubToolStripMenuItem.Checked = false;  

            Logger.log("Action ADD - Router");
            designerForm.NetworkDesigner.NetworkDesign.Action = DesignerAction.Add;
            designerForm.NetworkDesigner.NetworkDesign.DeviceType = DeviceType.Router;
        }

        private void switchToolStripMenuItem_Click(object sender, EventArgs e)
        {
            this.toolStripDropDownButton1.Image = IPSim.Properties.Resources.switch32;
            this.toolStripButton1.Checked = false;
            this.toolStripButton2.Checked = false;
            this.toolStripButton3.Checked = false;
            this.computerToolStripMenuItem.Checked = false;
            this.routerToolStripMenuItem.Checked = false;
            this.hubToolStripMenuItem.Checked = false;  

            Logger.log("Action ADD - Switch");
            designerForm.NetworkDesigner.NetworkDesign.Action = DesignerAction.Add;
            designerForm.NetworkDesigner.NetworkDesign.DeviceType = DeviceType.Switch;
        }

        private void hubToolStripMenuItem_Click(object sender, EventArgs e)
        {
            this.toolStripDropDownButton1.Image = IPSim.Properties.Resources.hub32;
            this.toolStripButton1.Checked = false;
            this.toolStripButton2.Checked = false;
            this.toolStripButton3.Checked = false;
            this.computerToolStripMenuItem.Checked = false;
            this.routerToolStripMenuItem.Checked = false;
            this.switchToolStripMenuItem.Checked = false;

            Logger.log("Action ADD - Hub");
            designerForm.NetworkDesigner.NetworkDesign.Action = DesignerAction.Add;
            designerForm.NetworkDesigner.NetworkDesign.DeviceType = DeviceType.Hub;
        }

        private void toolStripButton2_Click(object sender, EventArgs e)
        {
            this.toolStripButton1.Checked = false;
            this.toolStripButton3.Checked = false;
            this.computerToolStripMenuItem.Checked = false;
            this.routerToolStripMenuItem.Checked = false;
            this.switchToolStripMenuItem.Checked = false;
            this.hubToolStripMenuItem.Checked = false;  

            Logger.log("Action Delete");
            designerForm.NetworkDesigner.NetworkDesign.Action = DesignerAction.Delete;
        }

        private void toolStripButton3_Click(object sender, EventArgs e)
        {
            this.toolStripButton1.Checked = false;
            this.toolStripButton2.Checked = false;
            this.computerToolStripMenuItem.Checked = false;
            this.routerToolStripMenuItem.Checked = false;
            this.switchToolStripMenuItem.Checked = false;
            this.hubToolStripMenuItem.Checked = false;

            Logger.log("Action Connect");
            designerForm.NetworkDesigner.NetworkDesign.Action = DesignerAction.Connect;
        }

        private void toolStripButton4_Click(object sender, EventArgs e)
        {
            if (toolStripButton4.Checked)
                ShowLogScreen();
            else
                HideLogScreen();
        }

        private void toolStripButton5_Click(object sender, EventArgs e)
        {
            designerForm.NetworkDesigner.NetworkDesign.ShowGrid = toolStripButton5.Checked;
        }

        private void design_PropertyChanged(object sender, EventArgs e)
        {
            if (designerForm.NetworkDesigner.NetworkDesign.SelectedDevices.Count > 0)
            {
                this.propertiesForm.PropertyGrid.SelectedObject = designerForm.NetworkDesigner.NetworkDesign.SelectedDevices[0];
                this.consoleForm.DeviceConsole.TryToAttachDevice(designerForm.NetworkDesigner.NetworkDesign.SelectedDevices[0]);
            }
            else
                this.propertiesForm.PropertyGrid.SelectedObject = null;

            this.toolStripButton1.Checked = false;
            this.toolStripButton2.Checked = false;
            this.toolStripButton3.Checked = false;
            this.computerToolStripMenuItem.Checked = false;
            this.routerToolStripMenuItem.Checked = false;
            this.switchToolStripMenuItem.Checked = false;
            this.hubToolStripMenuItem.Checked = false;
            switch (designerForm.NetworkDesigner.NetworkDesign.Action)
            { 
                case DesignerAction.Add:
                    switch (designerForm.NetworkDesigner.NetworkDesign.DeviceType)
                    { 
                        case DeviceType.Computer:
                            this.computerToolStripMenuItem.Checked = true;
                            break;
                        case DeviceType.Hub:
                            this.hubToolStripMenuItem.Checked = true;
                            break;
                        case DeviceType.Router:
                            this.routerToolStripMenuItem.Checked = true;
                            break;
                        case DeviceType.Switch:
                            this.switchToolStripMenuItem.Checked = true;
                            break;
                        default:
                            break;
                    }
                    break;
                case DesignerAction.Connect:
                    this.toolStripButton3.Checked = true;
                    break;
                case DesignerAction.Select:
                    this.toolStripButton1.Checked = true;
                    break;
                case DesignerAction.Delete:
                    this.toolStripButton2.Checked = true;
                    break;
                default:
                    break;
            }


        }

        private void toolStripButton6_Click(object sender, EventArgs e)
        {
            if (toolStripButton6.Checked)
                ShowConsoleScreen();
            else
                HideConsoleScreen();
        }

        private void toolStripButton7_Click(object sender, EventArgs e)
        {
            if (toolStripButton7.Checked)
                ShowPropertiesScreen();
            else
                HidePropertiesScreen();
        }
    }
}